import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:get/get.dart';
import 'package:xindong/common/xindong_app_info.dart';
import 'package:xindong/xindong_getx_bangding.dart';
import 'package:xindong/xindong_main_yemian.dart';
import 'package:xindong/widgets/dialog/xindong_privacy_dialog.dart';

import 'common/xindong_all.dart';
import 'common/xindong_methods.dart';
import 'common/xindong_refresh.dart';
import 'common/xindong_zhuti.dart';
import 'manager/xindong_yinsi_singleton.dart';

class XinDongYingYong extends StatefulWidget {
  const XinDongYingYong({Key? key}) : super(key: key);

  @override
  State<XinDongYingYong> createState() => _XinDongYingYongState();
}

class _XinDongYingYongState extends State<XinDongYingYong> {
  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addPostFrameCallback((timeStamp) async {
      // 隐私协议
      bool agreePrivacy = await XinDongYinSiSingleton.to.firstLaunch();
      if (!agreePrivacy) {
        await XinDongPrivacyDialog.show();
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    return ScreenUtilInit(builder: (BuildContext context, child) {
      return XinDongRefreshComponents(
        child: GetMaterialApp(
          navigatorKey: navigatorKey,
          debugShowCheckedModeBanner: !XinDongAll.instance.isRelease,
          title: XinDongAppDeviceInfo.instance.showAppName,
          theme: XinDongZhuTi.theme,
          darkTheme: XinDongZhuTi.theme,
          defaultTransition: Transition.rightToLeft,
          getPages: [GetPage(name: "/", page: () => const XinDongMainYeMian())],
          initialRoute: "/",
          initialBinding: XinDongGetXBangDing(),
          unknownRoute: GetPage(name: '/not_found', page: () => const XinDongGetXNotFoundYeMian(title: "404")),

          // 本地化
          localizationsDelegates: const [
            GlobalCupertinoLocalizations.delegate,
            GlobalMaterialLocalizations.delegate,
            GlobalWidgetsLocalizations.delegate,
          ],

          // 支持语言
          supportedLocales: const [Locale("zh", "CN")],
          // 智能dialog
          builder: FlutterSmartDialog.init(builder: (BuildContext context, Widget? child) {
            XinDongAll.instance.smartDialogInitialized = true;
            return MediaQuery(
              data: MediaQuery.of(context).copyWith(textScaleFactor: 1.0),
              child: child!,
            );
          }),
        ),
      );
    });
  }
}
